import requests
import downloadimg
import getinfo
from bs4 import BeautifulSoup
import threading

url = "http://neuromorpho.org/getdataforbyspecies.jsp?species=chimpanzee"

header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"
}


def thread1():
    for a in A[1:200]:
        rel = str(a.get('rel')[0])
        href1 = f'http://neuromorpho.org/{rel.strip("./")}'
        downloadimg.download_img(href1)
        no = a.text
        href2 = f'http://neuromorpho.org/neuron_info.jsp?neuron_name={str(no).strip()}'
        getinfo.getinfom(href2)
    print('---子线程1结束---')


def thread2():
    for a in A[200:400]:
        rel = str(a.get('rel')[0])
        href1 = f'http://neuromorpho.org/{rel.strip("./")}'
        downloadimg.download_img(href1)
        no = a.text
        href2 = f'http://neuromorpho.org/neuron_info.jsp?neuron_name={str(no).strip()}'
        getinfo.getinfom(href2)
    print('---子线程2结束---')


def thread3():
    for a in A[400:600]:
        rel = str(a.get('rel')[0])
        href1 = f'http://neuromorpho.org/{rel.strip("./")}'
        downloadimg.download_img(href1)
        no = a.text
        href2 = f'http://neuromorpho.org/neuron_info.jsp?neuron_name={str(no).strip()}'
        getinfo.getinfom(href2)
    print('---子线程3结束---')


def thread4():
    for a in A[600:800]:
        rel = str(a.get('rel')[0])
        href1 = f'http://neuromorpho.org/{rel.strip("./")}'
        downloadimg.download_img(href1)
        no = a.text
        href2 = f'http://neuromorpho.org/neuron_info.jsp?neuron_name={str(no).strip()}'
        getinfo.getinfom(href2)
    print('---子线程4结束---')


def thread5():
    for a in A[800:]:
        rel = str(a.get('rel')[0])
        href1 = f'http://neuromorpho.org/{rel.strip("./")}'
        downloadimg.download_img(href1)
        no = a.text
        href2 = f'http://neuromorpho.org/neuron_info.jsp?neuron_name={str(no).strip()}'
        getinfo.getinfom(href2)
    print('---子线程5结束---')


def main():
    t1 = threading.Thread(target=thread1)
    t1.start()
    t2 = threading.Thread(target=thread2)
    t2.start()
    t3 = threading.Thread(target=thread3)
    t3.start()
    t4 = threading.Thread(target=thread4)
    t4.start()
    t5 = threading.Thread(target=thread4)
    t5.start()
    t5.join()

    print('---主线程---结束')


if __name__ == '__main__':
    res = requests.get(url, headers=header)
    page = BeautifulSoup(res.text, "html.parser")
    A = page.find_all("a", class_="screenshot")
    main()
    res.close()
